package org.wenhuayuan.uilibrary.banner.core;

import android.content.Context;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import androidx.annotation.NonNull;
import androidx.viewpager.widget.ViewPager;

import org.wenhuayuan.uilibrary.R;
import org.wenhuayuan.uilibrary.banner.indicator.HiCircleIndicator;
import org.wenhuayuan.uilibrary.banner.indicator.HiIndicator;

import java.util.List;

public class HiBannerDelegate implements IHiBanner, ViewPager.OnPageChangeListener {
    private Context context;
    private HiBanner hiBanner;
    private HiBannerAdapter bannerAdapter;
    private HiIndicator<?> indicator;
    private boolean autoPlay;
    private boolean loop;
    private List<? extends HiBannerMo> bannerMos;
    private int intervalTime = 5000;
    private int duration = 3000;
    private ViewPager.OnPageChangeListener onPageChangeListener;
    private HiBanner.OnBannerClickListener onBannerClickListener;
    private HiViewPager viewPager;

    public HiBannerDelegate(Context context, HiBanner hiBanner) {
        this.context = context;
        this.hiBanner = hiBanner;
    }

    @Override
    public void setBannerData(int layoutResId, @NonNull List<? extends HiBannerMo> models) {
        this.bannerMos = models;
        initData(layoutResId);
    }

    private void initData(int layoutResId) {
        if (this.bannerAdapter == null) {
            this.bannerAdapter = new HiBannerAdapter(context);
        }

        if (null == this.indicator) {
            this.indicator = new HiCircleIndicator(context);
        }
        this.indicator.onInflate(this.bannerMos.size());
        this.bannerAdapter.setLayoutResId(layoutResId);
        this.bannerAdapter.setLoop(this.loop);
        this.bannerAdapter.setAutoPlay(this.autoPlay);
        this.bannerAdapter.setBannerData(this.bannerMos, this.viewPager);
        this.bannerAdapter.setOnBannerClickListener(onBannerClickListener);

        this.viewPager = new HiViewPager(context);
        this.viewPager.setAutoPlay(autoPlay);
        this.viewPager.setIntervalTime(intervalTime);
        this.viewPager.setAdapter(this.bannerAdapter);
        this.viewPager.addOnPageChangeListener(this);
        if(duration > 0) this.viewPager.setScrollDuration(duration);
        if((autoPlay && loop) && this.bannerAdapter.getCount() > 0 ) {
            // 无线轮播的关键：使得第一张能滑动到最后一张，以达到无线轮播效果
            int firstItem = this.bannerAdapter.getFirstItem();
            this.viewPager.setCurrentItem(firstItem, false); // 第一次，无动画
        }
        // 清空所有的banner中缓存的view
        this.hiBanner.removeAllViews();
        FrameLayout.LayoutParams params =
                new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                        ViewGroup.LayoutParams.MATCH_PARENT);
        this.hiBanner.addView(this.viewPager, params);
        this.hiBanner.addView(this.indicator.get(), params);
    }

    @Override
    public void setBannerData(@NonNull List<? extends HiBannerMo> models) {
        setBannerData(R.layout.hi_banner_item_image, models);
    }

    @Override
    public void setHiIndicator(HiIndicator<?> hiIndicator) {
        this.indicator = hiIndicator;
    }

    @Override
    public void setAutoPlay(boolean autoPlay) {
        this.autoPlay = autoPlay;
        if (this.bannerAdapter != null) {
            this.bannerAdapter.setAutoPlay(autoPlay);
        }
        if (this.viewPager != null) {
            this.viewPager.setAutoPlay(autoPlay);
        }
    }

    @Override
    public void setLoop(boolean loop) {
        this.loop = loop;
//        if (this.bannerAdapter != null) {
//            this.bannerAdapter.setLoop(loop);
//        }
    }

    @Override
    public void setIntervalTime(int intervalTime) {
        if(intervalTime > 0)
            this.intervalTime = intervalTime;
    }

    @Override
    public void setBindAdapter(IBindAdapter bindAdapter) {
        this.bannerAdapter.setBindAdapter(bindAdapter);
    }

    @Override
    public void setOnPageChangeListener(ViewPager.OnPageChangeListener onPageChangeListener) {
        this.onPageChangeListener = onPageChangeListener;
    }

    @Override
    public void setOnBannerClickListener(OnBannerClickListener onBannerClickListener) {
        this.onBannerClickListener = onBannerClickListener;
    }

    @Override
    public void setScrollDuration(int duration) {
        this.duration = duration;
        if(viewPager != null && duration > 0) {
            viewPager.setScrollDuration(duration);
        }
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
        if (onPageChangeListener != null && bannerAdapter.getCount() > 0) {
            onPageChangeListener.onPageScrolled(position % bannerAdapter.getRealCount(), positionOffset, positionOffsetPixels);
        }
    }

    @Override
    public void onPageSelected(int position) {
        if (this.bannerAdapter != null && this.bannerAdapter.getRealCount() > 0) {
            position = position % this.bannerAdapter.getRealCount();
            if(null != onPageChangeListener) {
                onPageChangeListener.onPageSelected(position);
            }
            if (null != indicator) {
                indicator.onPointChange(position, this.bannerAdapter.getRealCount());
            }
        }

    }

    @Override
    public void onPageScrollStateChanged(int state) {
        if(null != onPageChangeListener) {
            onPageChangeListener.onPageScrollStateChanged(state);
        }
    }
}
